/**
 * 查询条件生成器
 * 将POST过来的JSON筛选器条件对象转换为接口需要的filter数组
 */

'use strict';

const NIL = '-1';

const builder = {
	build: (condition) => {
		let filters = [];

		if (condition.province) {
			filters.push(builder.append('teacher-province_id', '=', condition.province));
		}
		if (condition.city) {
			filters.push(builder.append('teacher-city_id', '=', condition.city));
		}
		if (condition.county) {
			filters.push(builder.append('teacher-county_id', '=', condition.county));
		}
		if (condition.school) {
			filters.push(builder.append('teacher-school_id', '=', condition.school));
		}
		if (condition.stage[0] != NIL) {
			filters.push(builder.append('teacher-grade_part', 'in', condition.stage.join(',')));
		}
		if (condition.grade[0] != NIL) {
			filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
		}
		if (condition.subject[0] != NIL) {
			filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
		}
		if (condition.version != '0') {
			filters.push(builder.append('teacher-version', '=', condition.version));
		}
		
		filters.push(builder.append('homework-day', '>=', condition.startDate));
		filters.push(builder.append('homework-day', '<=', condition.endDate));
		filters.push(builder.append('teacher-student_count', '>=', condition.userFeature.start));
		filters.push(builder.append('teacher-student_count', '<=', condition.userFeature.end));

		return filters;
	},
	append: (item, type, value) => {
		return {
			item,
			type,
			value,
		};
	}
}

module.exports = builder;